/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.insta.browser.view.tab;

import fr.insta.browser.service.history.IHistorySingleTabService;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.Timer;

/**
 * <p>HeaderView class.</p>
 *
 * @author Maarek
 * @version $Id: $Id
 */
public class HeaderView extends javax.swing.JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    final private IHistorySingleTabService historyService;
    private Timer timerPrevious;
    private Timer timerNext;
    private Timer timerHome;
    private HeaderViewDelegate delegate;

    /**
     * <p>Constructor for HeaderView.</p>
     *
     * @param historyService a {@link fr.insta.browser.service.history.IHistorySingleTabService} object.
     */
    public HeaderView(IHistorySingleTabService historyService) {
        super();
        this.historyService = historyService;
        initComponents();
        connectListeners();
    }

    private void connectListeners() {
        timerPrevious = new Timer(500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timerPrevious.stop();
                if (delegate != null) {
                    delegate.onLongClickPrevious(HeaderView.this);
                }
            }
        });

        timerNext = new Timer(500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timerNext.stop();
                if (delegate != null) {
                    delegate.onLongClickNext(HeaderView.this);
                }
            }
        });

        timerHome = new Timer(500, new ActionListener() {
        	@Override
        	public void actionPerformed(ActionEvent e) {
        		timerHome.stop();
        		if (delegate != null) {
        			delegate.onLongClickHome(HeaderView.this);
        		}
        	}
        });
    }

    private void initComponents() {

        prevButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        homeButton = new javax.swing.JButton();
        locationTextField = new javax.swing.JTextField();
        jToolBar1 = new javax.swing.JToolBar();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        
        refreshButton.setIcon(new ImageIcon(this.getClass().getResource("/img/refresh.png")));
        ImageIcon test  = new ImageIcon(this.getClass().getResource("/img/home.png"));
        homeButton.setIcon(new ImageIcon(this.getClass().getResource("/img/home.png")));
        prevButton.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/img/return.png")).getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));
        nextButton.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/img/avancer.png")).getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));

        homeButton.setToolTipText("Afficher la page d'accueil");
        prevButton.setToolTipText("Afficher la page précédente");
        nextButton.setToolTipText("Afficher la page suivante");
        refreshButton.setToolTipText("Rafraîchir la page");

        prevButton.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mousePressed(java.awt.event.MouseEvent evt) {
                prevButtonMousePressed(evt);
            }
            @Override
			public void mouseReleased(java.awt.event.MouseEvent evt) {
                prevButtonMouseReleased(evt);
            }
        });
        prevButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                prevButtonActionPerformed(evt);
            }
        });


        nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mousePressed(java.awt.event.MouseEvent evt) {
                nextButtonMousePressed(evt);
            }
            @Override
			public void mouseReleased(java.awt.event.MouseEvent evt) {
                nextButtonMouseReleased(evt);
            }
        });
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        locationTextField.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                locationTextFieldActionPerformed(evt);
            }
        });
        
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });
        
        //homeButton.setText("Accueil");
        homeButton.addMouseListener(new java.awt.event.MouseAdapter() {
        	@Override
			public void mousePressed(java.awt.event.MouseEvent evt) {
        		homeButtonMousePressed(evt);
        	}
        	@Override
			public void mouseReleased(java.awt.event.MouseEvent evt) {
        		homeButtonMouseReleased(evt);
        	}
        });
        homeButton.addActionListener(new java.awt.event.ActionListener() {
        	@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
        		homeButtonActionPerformed(evt);
        	}
        });

        jToolBar1.setRollover(true);

        jButton3.setText("Google");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton3);

        jButton4.setText("Site du Zero");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton4);

        jButton5.setText("Insta");
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton5);

        jButton6.setText("Java");
        jButton6.setFocusable(false);
        jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton6);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(prevButton)
                        .addGap(0, 0, 0)
                        .addComponent(nextButton)
                        .addGap(0, 0, 0)
                        .addComponent(locationTextField)
                        .addGap(0, 0, 0)
                        .addComponent(homeButton)
                        .addGap(0, 0, 0)
                        .addComponent(refreshButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prevButton)
                    .addComponent(nextButton)
                    .addComponent(locationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(homeButton)
                    .addComponent(refreshButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                .addContainerGap())
        );
    }

    private void prevButtonMousePressed(java.awt.event.MouseEvent evt) {                                        
        timerPrevious.start();
    }                                       

    private void prevButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
        timerPrevious.stop();
    }                                        

    private void nextButtonMousePressed(java.awt.event.MouseEvent evt) {                                        
        timerNext.start();
    }                                       

    private void nextButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
    	timerNext.stop();
    }                                        
    
    private void homeButtonMousePressed(java.awt.event.MouseEvent evt) {                                        
    	timerHome.start();
    }                                       
    
    private void homeButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
    	timerHome.stop();
    }                                        

    private void prevButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if (delegate != null) {
            delegate.onClickPrevious(this);
        }
    }                                          

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if (delegate != null) {
            delegate.onClickNext(this);
        }
    }                                          

    private void locationTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        if (delegate != null) {
            delegate.onLocationTextFieldActionPerformed(this, evt);
        }
    }                                                 

    private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    	if (delegate != null) {
    		delegate.onClickHome(this);
    	}
    }                                          

    
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if (delegate != null) {
            delegate.onClickRefresh();
        }
    }                                             
    // <editor-fold defaultstate="collapsed" desc="Getter Setter">
    /**
     * <p>Setter for the field <code>delegate</code>.</p>
     *
     * @param delegate a {@link fr.insta.browser.view.tab.HeaderViewDelegate} object.
     */
    public void setDelegate(HeaderViewDelegate delegate) {
        this.delegate = delegate;
    }

    /**
     * <p>Getter for the field <code>locationTextField</code>.</p>
     *
     * @return a {@link javax.swing.JTextField} object.
     */
    public JTextField getLocationTextField() {
        return locationTextField;
    }

    /**
     * <p>Getter for the field <code>nextButton</code>.</p>
     *
     * @return a {@link javax.swing.JButton} object.
     */
    public JButton getNextButton() {
        return nextButton;
    }

    /**
     * <p>Getter for the field <code>prevButton</code>.</p>
     *
     * @return a {@link javax.swing.JButton} object.
     */
    public JButton getPrevButton() {
        return prevButton;
    }

    /**
     * <p>Getter for the field <code>homeButton</code>.</p>
     *
     * @return a {@link javax.swing.JButton} object.
     */
    public JButton getHomeButton() {
    	return homeButton;
    }

    // </editor-fold>

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField locationTextField;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton prevButton;
    private javax.swing.JButton homeButton;
    private javax.swing.JButton refreshButton;
    // End of variables declaration                   
}
